.PHONY: list

list:
	@$(foreach x,$(LIBS),printf "%-10s - %s\n" "$(x)" "$(notdir $($(x)-srcf))";)


# All library phonys
.PHONY: all
all: $(LIBS)

define eval_phonys
.PHONY: $$(addsuffix .$(1),all $(LIBS))
all.$(1): $$(addsuffix .$(1),$(LIBS))
endef

PHONYS = unpack clean config remove install uninstall
$(foreach x,$(PHONYS),$(eval $(call eval_phonys,$(x))))


# Echo for each phony targets
define echo
@printf '  \033[32m[$(1)]\033[0m $(NAME)\n'
endef


# Make new directory
%/.mkdir:
	$(V)mkdir -p $(@D)
	$(V)touch $@

# Auto evaluate library dependency
define eval_libs
$(1)-src ?= $$(abspath $$(wildcard $(SRC_DIR)/$(1)*.tar.*))
$(1)-srcf ?= $$(notdir $$($(1)-src))
$(1)-dir ?= $$(call tar-base,$$($(1)-srcf))
$(1)-dst ?= /tmp/$$($(1)-dir)
$(1)-lib ?= lib$(1).so
$(1)-out ?= \
	$$(addprefix $$(PREFIX)/bin/,$$($(1)-bin)) \
	$$(addprefix $$(PREFIX)/include/,$$($(1)-h)) \
	$$(addprefix $$(PREFIX)/lib/,$$($(1)-lib))

$(1) $(1).%: NAME = $(1) ($$($(1)-srcf))
$(1) $(1).%: SRC = $$($(1)-src)
$(1) $(1).%: DST = $$($(1)-dst)
$(1) $(1).%: OUT = $$($(1)-out)

$(1).unpack    ?= $(auto.unpack)
$(1).clean     ?= $(auto.clean)
$(1).config    ?= $(auto.config)
$(1).build     ?= $(auto.build)
$(1).install   ?= $(auto.install)
$(1).uninstall ?= $(auto.uninstall)
$(1).remove    ?= $(auto.remove)
$(1).strip     ?= $(auto.strip)

$$($(1)-out): $(PREFIX)/.mkdir
	$$(call echo,UNPACK)
	$$($(1).unpack)
	$$(call echo,CONFIG)
	$$($(1).config)
	$$(call echo,BUILD)
	$$($(1).build)
	$$(call echo,INSTALL)
	$$($(1).install)
	$$(call echo,STRIP)
	$$($(1).strip)
ifneq ($(REMOVE),N)
	$$(call echo,REMOVE)
	$$($(1).remove)
endif

$(1): $$($(1)-out)

$(1).clean:
	$$(call echo,CLEAN)
	$$($(1).clean)

$(1).unpack:
	$$(call echo,UNPACK)
	$$($(1).unpack)

$(1).config:
	$$(call echo,CONFIG)
	$$($(1).config)

$(1).install:
	$$(call echo,INSTALL)
	$$($(1).install)

$(1).uninstall:
	$$(call echo,UNINSTALL)
	$$($(1).uninstall)

$(1).remove:
	$$(call echo,REMOVE)
	$$($(1).remove)
endef

$(foreach x,$(LIBS),$(eval $(call eval_libs,$(x))))
